const errorRouter = [
  {
    path: '/main/home',
    name: 'main:home',
    component: () => import('@/views/pages/home/index.vue')
  },
  {
    path: '/main',
    redirect: '/main/home'
  },
  {
    path: '/main/',
    redirect: '/main/home'
  },
  {
    path: '/main/:catchAll(.*)',
    name: 'main:not:found',
    component: () => import('@/views/error/error/index.vue')
  },
  {
    path: '/main/:pathMatch(.*)*',
    redirect: '/main/'
  }
];

export const readApi = function () {
  const files = import.meta.glob('@/views/pages/**/**.vue');
  // const files0 = import.meta.glob('@/views/pages/**/**.vue', {eager: true});

  const routerMap: any = {};
  const routerArray: any[] = [];
  // console.log('files = ', files);
  Object.keys(files).forEach(path => {
    let url = path.substring(1, path.length - 4);
    url = url.replace(/src\/views\/pages/, '');
    url = url.replace(/\/index$/, '');
    // console.log('file = ', files[path]);
    const router = {
      path: '/main' + url,
      name: getUuid(),
      // component: files[path],
      component: files[path],
      children: []
    };
    // files[path]().then((component) => {
    //   const componentName = component.default.name || '';
    //   const componentTitle = component.default.title || '';
    //   console.log('url=', url, '\t componentName=', componentName, '\t componentTitle=', componentTitle);
    // });
    routerArray.push(router);
    routerMap[url] = router;
  });
  const routers = [];
  for (let i = 0; i < routerArray.length; i++) {
    const router = routerArray[i];
    const path = router.path;
    if (!path.match(/^.*\/components\/.*$/)) {
      const parent = findParentByPath(routerMap, router.path);
      if (parent !== undefined && parent !== null) {
        parent.children.push(router);
      } else {
        routers.push(router);
      }
    }
  }
  routers.push(...errorRouter);
  console.log('routers = ', routers);
  return routers;
};

export const getUuid = function (): string {
  return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = Math.random() * 16 | 0;
    const v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
};

export const findParentByPath = function (routerMap: any, url: string): any {
  const split = url.split('/');
  for (let i = 0; i < split.length; i++) {
    let parentPath = undefined;
    const lastPathSuffix = split[split.length - i - 1];
    const suffixPath = '/' + lastPathSuffix;
    const indexOf = url.indexOf(suffixPath);
    if (indexOf > 0) {
      parentPath = url.substring(0, indexOf);
    } else if (indexOf === 0) {
      parentPath = suffixPath;
    }
    if (parentPath !== undefined && parentPath !== null && parentPath !== '') {
      if (url.match(new RegExp('^' + parentPath + '/', 'g')) && url !== parentPath) {
        const router = routerMap[parentPath];
        if (router !== undefined && router !== null) {
          const routerPath = router.path;
          if (routerPath.match(/Router$/)) {
            return router;
          }
        }
      }
    }
  }
};